<?php
namespace Admin\Controller;
use Think\Controller;
class AnswerController extends CommonController {
    public function index(){

    	$res=M('answer t1')->join('question t2 on t1.qid=t2.id')->join('user t3 on t1.uid=t3.id')->where("astate='0'")->getField('t1.*, t2.title,t3.nickname');
    	$this->assign('answer',$res);
        $this->display();
    }
    //删除一条
    public function delone()
    {
        $aid=I('post.aid');
        $res=M('answer')->where("id='{$aid}'")->setField('astate','1');
        if ($res) {
            $arr = array(
                'error'=>0,
                'info'=>'删除成功!',
                );
            echo json_encode($arr);
            exit;
        }else{
            $arr = array(
                'error'=>0,
                'info'=>'删除失败!',
                );
            echo json_encode($arr);
            exit;
        }
    }

    public function delall()
    {
        //接到字符串 去最后一位 ',' 
        $num=rtrim($_POST['num'],',');
       	//判断如果为空
        if (empty($num)) {
        	$arr=array(
                'error'=>23,
                'info'=>'删除失败,您还没选中喔！'
                );
            echo json_encode($arr);
            exit();    //执行中断
        }

        $res=M('answer')->where("id in($num)")->setField('astate','1');


        //执行判断
        if($res){
            $arr=array(
                'error'=>0,
                'info'=>'删除成功'
                );
            echo json_encode($arr);
            exit();
        }else{
            $arr=array(
                'error'=>1,
                'info'=>'删除失败'
                );
            echo json_encode($arr);
            exit();
        };
    }

}
?>